<template>
  <div>
    <div class="tool-bar">
      <el-row :gutter="20" class="h_top">
        <el-form ref="form" inline label-width="70px" label-position="left" class="nb-form">
          <el-col :span="6">
            <el-form-item label="名称：">
              <el-input v-model="query.name" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row :gutter="20" class="bar-btn" style="margin-top: 30px">
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleAdd">新增</el-button>
        <el-button type="primary" icon="el-icon-delete" @click="delBtn">删除</el-button>
        <el-button type="primary" icon="el-icon-refresh-right" @click="refresh">刷新</el-button>
      </el-row>
    </div>
    <div class="nb-content">
      <el-table ref="multipleTable" v-loading="loading" border :data="departmentList" tooltip-effect="dark" class="nb-table" @selection-change="handleSelectionChange">
        <el-table-column label="序号" type="index" align="center" width="55" />
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column prop="create_time" label="创建日期" align="center" />
        <el-table-column label="操作" align="center">
          <template scope="{row}">
            <el-button type="primary" @click="goDetail(row.id)">编辑</el-button>
            <el-button type="danger" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" :page-size="query.limit" background layout="prev, pager, next" :total="data.total" :current-page.sync="query.page" @size-change="query" />
    </div>
  </div>
</template>

<script>
import API from '@/api/department/department'
export default {

  inject: ['reload', 'goUrl'],
  data() {
    return {
      radio: '全部',
      data: {
        dataList: [],
        total: 0
      },
      departmentList: [],
      query: {
        limit: 15,
        page: 1
      },
      loading: true,
      multipleSelection: []
    }
  },
  created() {
    this.index()
  },
  methods: {
    index() {
      API.departmentList(this.query).then(res => {
        this.departmentList = res.data.data
        this.data.total = res.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSearch() {
      this.query.page = 1
      this.index()
    },
    refresh() {
      this.reload()
    },
    goDetail(id) {
      const url = `/departmentEdit/${id}`
      this.goUrl(url)
    },
    handleAdd() {
      this.goUrl('/departmentAdd')
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete({ id }).then(res => {
          if (res.data.error_code !== 1) {
            this.$message.error(res.data.message)
          } else {
            this.$message.success('删除成功')
            this.reload()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delBtn() {
      console.log(this.multipleSelection)
      if (this.multipleSelection && this.multipleSelection.length > 0) {
        const idArray = []
        this.multipleSelection.forEach(item => {
          idArray.push(item.id)
        })
        const id = idArray.join(',')
        this.handleDel(id)
      } else {
        this.$message('请先选择，然后再删除')
      }
    }
  }
}
</script>

<style scoped>
.tool-bar{
  padding: 20px 22px;
  background-color: #fff;
  margin-bottom: 14px;
  border-radius: 5px;
}
.tool-input div{
  margin-bottom: 10px;
}
</style>
<style scoped>
  .nb-form .el-form-item__label{
    font-size: 14px;
    font-weight: normal;
  }
.nb-form .el-form-item__content{
  width: calc(100% - 70px);
}
</style>
